package cifrasong.cifra.gui;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import cifrasong.R;
import cifrasong.cifra.negocio.CifraService;
import cifrasong.usuario.dominio.Session;


/**
 * Created by Uehara on 20/10/2014.
 */
public class ExibeCifraAct extends ActionBarActivity {

    public static String shareWhats = null;
    final CifraService negocio = new CifraService(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exibe_cifra);

        final TextView cifra = (TextView)findViewById(R.id.letraCifra);

        Resources res = this.getResources();
        String conteudo = String.format(res.getString(R.string.cifra), Session.getCifraSelecionada().getArtista(), Session.getCifraSelecionada().getNome(),Session.getCifraSelecionada().getConteudo());
        cifra.setText(Html.fromHtml(conteudo));
        shareWhats = Html.fromHtml(conteudo).toString();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.exibe_cifra_salva, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.excluir) {
            try {
                negocio.deletarCifra();
                Toast.makeText(ExibeCifraAct.this, "Cifra deletada com sucesso.", Toast.LENGTH_SHORT).show();
                Intent i = new Intent();
                i.setClass(ExibeCifraAct.this,MinhaCifraAct.class);
                startActivity(i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (id == R.id.favoritar) {
            try {
                if (negocio.favoritarCifra(Session.getCifraSelecionada())) {
                    Intent intent = new Intent(this, FavoritoAct.class);
                    startActivity(intent);
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (id == R.id.share){
            try{
                //final ComponentName name = new ComponentName("com.whatsapp", "com.whatsapp.ContactPicker");
                Intent oShareIntent = new Intent(android.content.Intent.ACTION_SEND);
                //oShareIntent.setComponent(name);
                oShareIntent.setType("text/plain");
                oShareIntent.putExtra(Intent.EXTRA_TEXT, shareWhats + "Compartilhado via CifraSong");
                startActivity(oShareIntent);
            }catch(Exception e){
                Toast.makeText(ExibeCifraAct.this, "Ocorreu uma falha no compartilhamento.", Toast.LENGTH_SHORT).show();
            }
        }
        return super.onOptionsItemSelected(item);
    }
}